package com.ftl.gateway.api.v2.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

@Configuration
public class ResolverConfig {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    /**
     * ip限制
     * @return
     */
    @Bean
    public KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }

    /**
     * 用户限制
     * @return
     */
//    @Bean
    public KeyResolver userTokenResolver() {
        return exchange -> Mono.just(exchange.getRequest().getHeaders().getFirst(tokenHeader));
    }

    /**
     * 接口限制
     * @return
     */
//    @Bean
    public KeyResolver pathResolver() {
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }

}
